/*
 * =====================================================================================
 *
 *       Filename:  test.cc
 *
 *    Description:  test the nbtserver and client 
 *
 *        Version:  1.0
 *        Created:  2012年02月21日 11时54分12秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Regan (), lcqhigh@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */
#include "NameService.h"
#include <unistd.h>
#include <Thread.h>

void requestThread(void)
{
    NameService ns;

    string ipstr = "192.168.20.246";
    struct in_addr ipaddr;
    int res = inet_aton(ipstr.c_str(), &ipaddr);
    if (res == 0)
    {
        Debug(2, "convert the ip address failed\n");
        return;
    }

    ns.registerName("ubuntu", &ipaddr);

    struct in_addr  resip;
    char myhostname[32] = {'\0'};
    gethostname(myhostname, sizeof(myhostname)-1);
    ns.registerName(myhostname, &ipaddr);

    int count  = 0;
    string queryname = "skywalker";
    while (count < 3) {
        bool ret = ns.queryName(queryname, &resip);
        if (ret)
        {
            Debug(2, "the ip address of %s is %s\n", 
                    myhostname, inet_ntoa(resip));
        }
        else
        {
            Debug(2, "query the name for %s failed\n", myhostname);
        }
        ++count;
    }
    ns.run();
}

//void responceThread(void)
//{
//    NameService ns;
//    ns.run();
//}
//

int main()
{
    /*
    NameService ns;

    string ipstr = "192.168.20.17";
    struct in_addr ipaddr;
    int res = inet_aton(ipstr.c_str(), &ipaddr);
    if (res == 0)
    {
        Debug(2, "convert the ip address failed\n");
        return -1;
    }

    ns.registerName("20S", &ipaddr);

    struct in_addr  resip;
    char myhostname[32] = {'\0'};
    gethostname(myhostname, sizeof(myhostname)-1);
    int count  = 0;

    while (count < 3) {
        bool ret = ns.queryName(myhostname, &resip);
        if (ret)
        {
            Debug(2, "the ip address of %s is %s\n", 
                    myhostname, inet_ntoa(resip));
        }
        else
        {
            Debug(2, "query the name for %s failed\n", myhostname);
        }
        ++count;
        if (count == 1)
            strcpy(myhostname, "20S");
        else
            strcpy(myhostname, "30S");
    }
    */
//    Thread req_thread(requestThread);
//    Thread res_thread(responceThread);
//
//    req_thread.start();
//    res_thread.start();
//
//    req_thread.join();
//    res_thread.join();
     requestThread();
    return 0;
}
